package com.zml.demo;

import java.util.ArrayList;
import java.util.List;

public class PageList {
    public class Page {
        private long beginIndex;
        private long endIndex;

        public Page(long beginIndex, long endIndex) {
            this.beginIndex = beginIndex;
            this.endIndex = endIndex;
        }

        public long getBeginIndex() {
            return beginIndex;
        }

        public void setBeginIndex(long beginIndex) {
            this.beginIndex = beginIndex;
        }

        public long getEndIndex() {
            return endIndex;
        }

        public void setEndIndex(long endIndex) {
            this.endIndex = endIndex;
        }
    }

    private List<Page> pages = new ArrayList<Page>();

    public PageList(long minId, long maxId, int perSize) {
        if(minId > maxId)
            throw new RuntimeException("minId("+minId+") more than maxId("+maxId+")");

        long beginIndex = minId + 1;
        long endIndex = minId + (long)perSize;
        while (endIndex < maxId) {
            Page page = new Page(beginIndex, endIndex);
            pages.add(page);
            beginIndex += perSize;
            endIndex += perSize;
        }

        if(endIndex >= maxId) {
            endIndex = maxId;
            Page page = new Page(beginIndex, endIndex);
            pages.add(page);
        }

        long index = pages.get(0).getBeginIndex() - 1;
        pages.get(0).setBeginIndex(index);
    }

    public List<Page> getPages() {
        return pages;
    }
}